package com.ctos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctos.common.BaseContext;
import com.ctos.common.Result;
import com.ctos.entity.Orders;
import com.ctos.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders){

        ordersService.submit(orders);
        return Result.success("success");
    }

    /**
     * 查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page<Orders>> userPage(Integer page ,Integer pageSize){
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        ordersService.page(ordersPage);
        return Result.success(ordersPage);
    }

    /**
     * 查询所有订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public Result<Page<Orders>> page(Integer page ,Integer pageSize){
        Page<Orders> allPage = new Page<>(page,pageSize);
        ordersService.page(allPage);
        return Result.success(allPage);
    }

    /**
     * 配送订单
     * @return
     */
    @PutMapping
    @Transactional
    public Result<String> sendOrder(@RequestBody Orders orders){
        BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getId,orders.getId());
        Orders one = ordersService.getOne(ordersLambdaQueryWrapper);
        one.setStatus(4);
        ordersService.removeById(one);
        ordersService.save(one);
        return Result.success("success");
    }

}
